$(function()
{
	dislikesBG.browser = 'chrome';
	dislikesBG.jQuery = jQuery;
	dislikesBG.loadTranslations();
	dislikesBG.loadAds();
	dislikesBG.checkStatus();
	dislikesBG.sessionChkTimeout = setInterval('dislikesBG.checkStatus()', 30000);
});

//chrome.extension.onRequest.addListener(dislikesBG.onRequest);

var portsByName = {};

chrome.self.onConnect.addListener(function(port)
{
	portsByName[port.name] = port;
	
	port.onMessage.addListener(function(request)
	{
		if(request.analytics)
		{
			_gaq.push(['_trackEvent', 'Chrome', 'Reklama']);
			_gaq.push(['_trackPageview']);
		}
		else if(request.loggedIn === true || request.loggedIn === false)
		{
			dislikesBG.onRequest({"action": "stateChanged", "loggedIn": request.loggedIn});
		}
		else
		{
			dislikesBG.onRequest(request.params, port.name, request.params.callback);
		}
	});
	
	port.onDisconnect.addListener(function(request) {
		delete portsByName[request.name];
	});
});


dislikesBG.getBGurl = function(file)
{
	return chrome.extension.getURL('skin/'+file);
};

dislikesBG.HTMLParser = function(str)
{
	return str;
};

dislikesBG.loadOptions = function(callback, userID, doc)
{
	try 
	{			
		var lang = localStorage.getItem(userID+'_lang');
		if(lang == undefined || lang == null) throw {}
		
		dislikesBG.options.language = lang;
	} catch(e)	{}
	
	try 
	{			
		var firstTime = localStorage.getItem(userID+'_firstTime');
		if(firstTime == undefined || firstTime == null) throw {}
		
		dislikesBG.options.firstTime = firstTime;
	} catch(e)	{}
	
	try 
	{			
		var lastNotif = localStorage.getItem(userID+'_lastNotif');
		if(lastNotif == undefined || lastNotif == null) throw {}
		
		if(lastNotif == 'undefined') // wrong saving
			lastNotif = 0;
		
		dislikesBG.options.lastNotif = lastNotif;
	} catch(e)	{}
	
	try 
	{			
		var commentStuff = localStorage.getItem(userID+'_commentStuff');
		if(commentStuff == undefined || commentStuff == null) throw {}
		
		dislikesBG.options.commentStuff = commentStuff;
	} catch(e)	{}
	
	var sendOpt = dislikesBG.jQuery.extend({}, dislikesBG.options, {trans: dislikesBG.transObj, friends: dislikesBG.friendsData, session: dislikesBG.sessionData});
	
	callback(sendOpt);
};

dislikesBG.saveOptions = function(callback, userID, newOptions, doc)
{
	dislikesBG.options = dislikesBG.jQuery.extend({}, newOptions);
	
	try 
	{			
		localStorage.setItem(userID+'_lang', dislikesBG.options.language);
	} catch(e)	{}
	
	try 
	{			
		localStorage.setItem(userID+'_firstTime', dislikesBG.options.firstTime);
	} catch(e)	{}
	
	try 
	{			
		localStorage.setItem(userID+'_lastNotif', dislikesBG.options.lastNotif);
	} catch(e)	{}
	
	try 
	{			
		localStorage.setItem(userID+'_commentStuff', dislikesBG.options.commentStuff);
	} catch(e)	{}
	
	callback();
};